/**
 * Created by IanSalvatore on 2016/11/9 0009.
 */
$(function () {
    //加载购物车
    var LoadShopCar = function () {
        $("#CarProcess").show("normal");
        var options = {
            url: "/Home/Request",
            type: "POST",
            data: "account=" + $.cookie("User") + "&Ajax=" + "ShopCart",
            dataType: "Json"
        };
        $.ajax(options).done(function (obj) {
            var Foods = obj.FoodList;
            $.each(Foods, function (index, values) {
                var ElementHidden = "<input type='hidden' name='cartId' value='" + values.FId + "'/>";
                var ElementSubmit = "<span class='dBtn'></span>"
                var ElementForm = "<div>" + ElementHidden + ElementSubmit + "</div>";

                var ElementSpan = "<span>" + values.FName + "</span>";
                var ElementImg = "<img src='" + values.FImage + "'/>";
                var ElementA = "<a href='detail.html?id=" + values.FId + "'>" + ElementImg + ElementSpan + "</a>";
                var ElementAdiv = "<div>" + ElementA + "</div>";

                var ElementLi = "<li>" + ElementAdiv + ElementForm + "</li>";
                $("#CarList").append(ElementLi);
            })
            $("#CarProcess").hide("normal");
        })
        return false;
    }

    //加载购物车事件
    $(".carBtn").click(function () {
        LoadShopCar();
    })

    //删除购物车
    $("#CarList").delegate(".dBtn", "click", function () {
        var i = $(this);
        var href = i.prev('input').val()
        var options = {
            url: "/Home/Request",
            type: "POST",
            data: "fid=" + href + "&account=" + $.cookie("User") + "&Ajax=" + "DeleteInCart",
            dataType: "Json"
        };

        $.ajax(options).done(function (obj) {
            if (Boolean(obj.IsSuccess)) {
                i.parent().parent().remove();
            }
        })
    })

    //引入Jquery.Cookie插件
    if ($.cookie("User") != "null") {
        $(".SignOut").css("display", "block")
        $(".userstate").empty();
        $(".userstate").append("<a href='order.html' class='animated fadeInRight'><img src='img/avatar.jpg' class='animated rotateIn img-circle' width='30' style='position: relative;top: 9px;left: -20px;'/>账户管理</a>")
        //LoadShopCar()
    }
    else {
        $("#btnPayfor").text("亲，请登录").attr("href", "login.html");
        $(".SignOut").css("display", "none")
        $(".userstate").empty();
        $(".userstate").append("<a href='login.html' class='hrefLogin'>登录</a>");
        $(".userstate").append("<a href='login.html?iLogin=false'>注册</a>");
    }

    //注销
    $("#SignOut").click(function () {
        $.cookie("User", null, { path: '/' });
        window.location.reload();
    })

    var ajaxFormSubmit = function () {
        var $form = $(this);
        var typeId = $(this).attr("id");
        var options = {
            url: "/Home/Request",
            type: $form.attr("method"),
            data: $form.serialize() + "&Ajax=" + typeId,
            dataType: "Json"
        };

        $.ajax(options).done(function (obj) {
            //登录
            if (obj.JsonType == "Login") {
                //登录标识
                if (Boolean(obj.IsSuccess)) {
                    var account = $("#LoginAccount").val()
                    $.cookie("User", account, { path: '/' })
                    //$.cookie('User', obj.Account, { expires: 7 });
                    //登录成功跳转地址
                    window.location.href = obj.Result;
                }
                $(".validate-Auth-Login").text("用户名或密码无效");
                $(".validate-Login").addClass("fadeInDown")
            }
                //注册
            else if (obj.JsonType == "Register") {
                if (Boolean(obj.IsSuccess)) {
                    var account = $("#RegAccount").val()
                    $.cookie("User", account)
                    //注册成功跳转地址
                    window.location.href = obj.Result;
                    return;
                }
                $(".validate-Auth-Reg").text("用户名已存在，请使用其他用户名");
                $(".validate-Reg").addClass("fadeInDown")
            }
                //添加评价
            else if (obj.JsonType == "AddComment") {
                if (Boolean(obj.IsSuccess)) {
                    $(".CommentPanel").hide('normal')
                    var ElementImg = "<img class='img-circle' src='img/avatar.jpg' width='48'/>";
                    var ElementImgA = "<a class='pull-left'>" + ElementImg + "</a>";

                    var date = new Date();
                    var ElementUserName = "<h3 class='media-heading'>" + $.cookie("User") + "</h3>";
                    var ElementDate = "<p class='comment-date'>" + date.toLocaleString() + "</p>";
                    var ElementComment = "<p class='comment-content'>" + $("#ComInfo").val() + "</p>";
                    var ElementBody = "<div class='media-body'>" + ElementUserName + ElementDate + ElementComment + "</div>";

                    var ElementMedia = "<div class='media'>" + ElementImgA + ElementBody + "</div>";
                    $("#ComInfo").val("")
                    $(".CommentPanel").append(ElementMedia).append("<hr class='sm'>");
                    $(".CommentPanel").show('normal')
                }
            }
                //添加购物车
            else if (obj.JsonType == "AddInCart") {
                if (Boolean(obj.IsSuccess)) {
                    //无操作，前端未写其他提示
                }
                //未添加成功错误提示
            }

        })
        return false;
    }
    $("form[data-form-ajax='true']").submit(ajaxFormSubmit);

    
})